import 'package:bookshelf/entity/classify_entity.dart';
import 'package:bookshelf/entity/pic_entity.dart';
import 'package:bookshelf/entity/version_entity.dart';
import 'package:dio/dio.dart';

import '../entity/book_entity.dart';
import '../entity/borrow_again_entity.dart';
import '../entity/borrow_entity.dart';
import '../entity/face_search_entity.dart';
import '../entity/reader_entity.dart';
import '../entity/return_entity.dart';
import '../main.dart';
import '../util/toast_util.dart';
import 'api_url.dart';
import 'http_helper.dart';

String? operator = '';

Future<String?> register(String list) async {
  var data = await HttpHelper.instance.post(upUrl, data: list,options: Options(receiveTimeout: Duration.zero));
  if(data['code'] == 200){
    if(data.toString().contains("未检索到图书No")){
      var start = data.toString().indexOf('未检索到图书No');
      var end = data.toString().lastIndexOf(';');
      var string = data.toString().substring(start+9,end);
      return string.trim();
    }
    return '';
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}

Future up(String list) async {
  var data = await HttpHelper.instance.post(upUrl, data: list,options: Options(receiveTimeout: Duration.zero));
  if(data['code'] == 200){
    return true;
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return false;
  }else{
    toastWarn(data['message']);
    return false;
  }
}

Future<bool> resortById(id,count) async {
  var data = await HttpHelper.instance.post(resortUrl, data: {'id':id,'num':count},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    return true;
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return false;
  }else{
    toastWarn(data['message']);
    return false;
  }
}

Future<bool> resortByNo(no,count) async {
  var data = await HttpHelper.instance.post(resortUrl, data: {'no':no,'num':count},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    return true;
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return false;
  }else{
    toastWarn(data['message']);
    return false;
  }
}

Future setting(numMachine,remark) async {
  var data = await HttpHelper.instance.post(settingUrl, data: {'numMachine':numMachine,'remark':remark},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    return data['result'];
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}


Future<List<PicEntity>?> pic() async {
  var data = await HttpHelper.instance.post(picUrl, data: {'numMachine':"${settingEntity.roomNo}${settingEntity.columnNo}${settingEntity.main == true?'':'f'}"},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    List<dynamic> str = data['result'];
    List<PicEntity> listData = [];
    for (var element in str) {
      listData.add(PicEntity.fromJson(element));
    }
    return listData;
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}

Future<VersionEntity?> getVersion() async {
  var data = await HttpHelper.instance.post(getVersionUrl, data: {'softwareType':'bookself','numMachine':"${settingEntity.roomNo}${settingEntity.columnNo}${settingEntity.main == true?'':'f'}"},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    return VersionEntity.fromJson(data['result']);
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}
Future suggest(mainTitle,remark) async {
  var data = await HttpHelper.instance.post(suggestUrl, data: {'mainTitle':mainTitle,'remark':remark},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    return true;
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}
Future bind(no,cardno) async {
  var data = await HttpHelper.instance.post(bindIcrUrl, data: {'no':no,'cardno':cardno},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    return true;
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}

Future getReaderNo(cardno) async {
  var data = await HttpHelper.instance.post(getNoUrl, data: {'cardno':cardno},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    return data['result'];
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}

Future<List<BookEntity>?> recommend(range,pageSize) async {
  var data = await HttpHelper.instance.post(recommendUrl, data: {'range':range,'page':1,'pageSize':pageSize},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    List<dynamic> str = data['result']['items'];
    List<BookEntity> listData = [];
    for (var element in str) {
      listData.add(BookEntity.fromJson(element));
    }
    return listData;
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}

Future<List<BookEntity>?> readRank(range,pageSize) async {
  var data = await HttpHelper.instance.post(readRankUrl, data: {'range':range,'page':1,'pageSize':pageSize},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    List<dynamic> str = data['result']['items'];
    List<BookEntity> listData = [];
    for (var element in str) {
      listData.add(BookEntity.fromJson(element));
    }
    return listData;
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}

Future<List<BookEntity>?> search(range,page,{String classify = '',String keyword = ''}) async {
  var data = await HttpHelper.instance.post(queryUrl, data: {'range':range,'page':page,'pageSize':20,'classify':classify,'keyword':keyword},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    List<dynamic> str = data['result']['items'];
    List<BookEntity> listData = [];
    for (var element in str) {
      listData.add(BookEntity.fromJson(element));
    }
    return listData;
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}

Future<List<BookEntity>?> queryById(range,id,page) async {
  var data = await HttpHelper.instance.post(queryUrl, data: {'range':range,'id':id,'page':page},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    List<dynamic> str = data['result']['items'];
    List<BookEntity> listData = [];
    for (var element in str) {
      listData.add(BookEntity.fromJson(element));
    }
    return listData;
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}

Future<List<BookEntity>?> query(range) async {
  var data = await HttpHelper.instance.post(queryUrl, data: {'range':range,'pageSize':100000},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    List<dynamic> str = data['result']['items'];
    List<BookEntity> listData = [];
    for (var element in str) {
      listData.add(BookEntity.fromJson(element));
    }
    return listData;
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}

Future<List<ClassifyEntity>?> classify() async {
  var data = await HttpHelper.instance.get(classifyUrl,options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    List<dynamic> str = data['result'];
    List<ClassifyEntity> listData = [];
    for (var element in str) {
      listData.add(ClassifyEntity.fromJson(element));
    }
    return listData;
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}

Future getOperator() async {
  var data = await HttpHelper.instance.post(operatorUrl, data: {'numMachine':"${settingEntity.roomNo}${settingEntity.columnNo}${settingEntity.main == true?'':'f'}"},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(data['code'] == 200){
    return data['result'];
  }else if (data['code'] == 505){
    toastWarn("请求超时,请检查网络");
    return null;
  }else{
    toastWarn(data['message']);
    return null;
  }
}

Future<ReaderEntity?> findReader(readerNo) async {
  var response = await HttpHelper.instance.post(findReaderUrl, data: {'device_id': 'rfid_cabinet_001', 'reader_no': readerNo,'password':'000000','category':''},options: Options(receiveTimeout: const Duration(seconds: 10)));

  if(response['code'] == 200){
    var data  = ReaderEntity.fromJson(response['result']);
    return data;
  }else{
    toastWarn("${response['message']}");
    return null;
  }
}


Future readerApply(readerNo,name,id) async {
  var response = await HttpHelper.instance.post(readerApplyUrl,
      data: {'device_id': 'rfid_cabinet_001', 'reader_no': readerNo,'reader_name':name,'icr_no':id, 'password':'000000','uid':readerNo},options: Options(receiveTimeout: const Duration(seconds: 10)));

  if(response['code'] == 200){
    return true;
  }else{
    toastWarn("${response['message']}");
    return false;
  }

}

Future<BorrowEntity?> bookBorrow(readerNo,bookNo) async {
  var response = await HttpHelper.instance.post(borrowUrl, data: {'device_id': 'rfid_cabinet_001', 'reader_no': readerNo,'book_no':bookNo,'operator':operator},options: Options(receiveTimeout: const Duration(seconds: 10)));

  if(response['code'] == 200){
    var data  = BorrowEntity.fromJson(response['result']);
    return data;
  }else {
    toastWarn("${response['message']}");
    return null;
  }

}

Future<ReturnEntity?> bookReturn(bookNo) async {
  var response = await HttpHelper.instance.post(returnUrl, data: {'device_id': 'rfid_cabinet_001','book_no':bookNo,'operator':operator},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(response['code'] == 200){
    var data  = ReturnEntity.fromJson(response['result']);
    return data;
  }else {
    toastWarn("${response['message']}");
    return null;
  }
}

Future bookBorrowAgain(readerNo,bookNo) async {
  var response = await HttpHelper.instance.post(borrowAgainUrl, data: {'device_id': 'rfid_cabinet_001', 'reader_no': readerNo,'book_no':bookNo,'operator':operator},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(response['code'] == 200){
    var data  = BorrowAgainEntity.fromJson(response['result']);
    return (200,data);
  }else {
    return (400,response['message']);
  }
}

Future faceAdd(userId,userName,img) async {
  var (status,response) = await HttpHelper.instance.oldPost(faceAddUrl, data: {'user_id': userId, 'user_name': userName,'image_base64':img},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    return (true,null);
  }else{
    return (false, response['error']);
  }

}

Future faceSearch(img) async {
  var (status,response) = await HttpHelper.instance.oldPost(faceSearchUrl, data: {'image_base64':img},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    var data  = FaceSearchEntity.fromJson(response);
    return (data,null);
  }else{
    return (null, response['error']);
  }

}
Future<bool> faceQuery(userId) async {
  var (status,response) = await HttpHelper.instance.oldPost(faceQueryUrl, data: {"user_id": userId},options: Options(receiveTimeout: const Duration(seconds: 10)));
  if(status == 200){
    bool isEmpty  = response['empty'];
    return !isEmpty;
  }else{
    // toastWarn('${response['error']}');
    return false;
  }

}

Future<int?> days() async {
  var (status,response) = await HttpHelper.instance.oldPost(daysUrl, data: {"device_id": '123123'});
  if(status == 200){
    int day  = response['data']['days'];
    return day;
  }else{
    // toastWarn('${response['error']}');
    return null;
  }

}

